package com.camartsApp.service.impl;

import com.camartsApp.dto.PhotoListInfoDTO;
import com.camartsApp.entity.PhotoListEntity;
import com.camartsApp.entity.Post;
import com.camartsApp.mapper.PhotoListMapper;
import com.camartsApp.service.IPhotoListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PhotoListServiceImpl implements IPhotoListService {

    @Autowired
    private PhotoListMapper photoListMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<PhotoListInfoDTO> getRecentPhotos(int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<PhotoListEntity> photos = photoListMapper.findRecentPhotos(offset, pageSize);
        return convertToPhotoListInfoDTO(photos);
    }

    @Override
    public List<PhotoListInfoDTO> getRecentPhotosByUserId(String userId, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<PhotoListEntity> photos = photoListMapper.findRecentPhotosByUserId(userId, offset, pageSize);
        return convertToPhotoListInfoDTO(photos);
    }

    @Override
    public List<PhotoListInfoDTO> getHottestPhoto(int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<PhotoListEntity> photos = photoListMapper.findHottestPhoto(offset, pageSize);
        return convertToPhotoListInfoDTO(photos);
    }

    @Override
    public List<PhotoListInfoDTO> getUserHottestPhotos(String userId, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<PhotoListEntity> photos = photoListMapper.findUserHottestPhotos(userId, offset, pageSize);
        return convertToPhotoListInfoDTO(photos);
    }

    private List<PhotoListInfoDTO> convertToPhotoListInfoDTO(List<PhotoListEntity> photos) {
        if (photos == null || photos.isEmpty()) {
            return List.of();
        }

        List<String> photoIds = photos.stream()
                .map(PhotoListEntity::getId)
                .collect(Collectors.toList());

        Map<String, String> titleMap = getTitlesFromMongo(photoIds);

        return photos.stream()
                .map(photo -> new PhotoListInfoDTO(
                        photo.getId(),
                        photo.getUrl(),
                        titleMap.get(photo.getId()),
                        photo.getLatitude(),
                        photo.getLongitude(),
                        photo.getTakenAt()
                ))
                .collect(Collectors.toList());
    }

    private Map<String, String> getTitlesFromMongo(List<String> photoIds) {
        Query query = new Query(Criteria.where("imageId").in(photoIds.stream()
                .map(Long::valueOf)
                .collect(Collectors.toList())));
        List<Post> posts = mongoTemplate.find(query, Post.class);
        return posts.stream()
                .collect(Collectors.toMap(
                        post -> post.getImageId().toString(),
                        Post::getTitle,
                        (existing, replacement) -> existing // 如果有重复的key，保留第一个
                ));
    }
}